package org.jeecg.modules.gqHealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.gqHealth.entity.GqCardBatch;
import org.jeecg.modules.gqHealth.entity.GqCardManager;
import org.jeecg.modules.gqHealth.entity.GqStatisticsConfig;
import org.jeecg.modules.gqHealth.entity.GqUserCard;
import org.jeecg.modules.gqHealth.mapper.GqCardBatchMapper;
import org.jeecg.modules.gqHealth.mapper.GqCardManagerMapper;
import org.jeecg.modules.gqHealth.mapper.GqRealnameInfoMapper;
import org.jeecg.modules.gqHealth.mapper.GqUserCardMapper;
import org.jeecg.modules.gqHealth.service.IGqCardManagerService;
import org.jeecg.modules.gqHealth.service.IGqRealnameInfoService;
import org.jeecg.modules.utils.CardGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @Description: 体检卡表管理
 * @Author: jeecg-boot
 * @Date:   2024-05-12
 * @Version: V1.0
 */
@Service
public class GqCardManagerServiceImpl extends ServiceImpl<GqCardManagerMapper, GqCardManager> implements IGqCardManagerService {

    @Resource
    private GqCardManagerMapper cardManager;
    @Resource
    private GqCardBatchMapper cardBatchMapper;

    @Resource
    private GqUserCardMapper userCardMapper;

    @Autowired
    private IGqRealnameInfoService gqRealnameInfoService;

    @Override
    public Integer generateCard(String no, Integer num) {
        GqCardBatch gqCardBatch = cardBatchMapper.selectByBatchNo(no);
        if (null == gqCardBatch){
            return  -1;
        }
        List<GqCardManager> list = CardGenerator.generateCard(num);
        if (null == list || list.isEmpty()){
            return  -2;
        }

        return cardManager.addCard(gqCardBatch.getMonth(),gqCardBatch.getNumber(),gqCardBatch.getNo(),gqCardBatch.getName(),list);
    }

    @Override
    public Integer bindUserCard(String no, String code) {
        QueryWrapper<GqCardManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_no",no);
        queryWrapper.isNull("active_time");
        if (StringUtils.isNotEmpty(code)){
            queryWrapper.eq("security_code",code);
        }
        List<GqCardManager> gqCardManagers = cardManager.selectList(queryWrapper);
        if (null == gqCardManagers || gqCardManagers.isEmpty()){
            return  -1;
        }
        GqCardManager bean = gqCardManagers.get(0);
        bean.setActiveTime(new Date());
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 添加三个月
        LocalDate threeMonthsLater = currentDate.plusMonths(bean.getMonth());
        Date date = Date.from(threeMonthsLater.atStartOfDay(ZoneId.systemDefault()).toInstant());

        bean.setEndTime(date);
        if (updateById(bean)){
            GqUserCard card = new GqUserCard();
            card.setCardNo(no);
            userCardMapper.insert(card);
        }else{
            return  -1;
        }
        return 1;
    }

    @Override
    public List<GqCardManager> selectUserCard() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return cardManager.selectUserCard(sysUser.getUsername());
    }

    @Override
    public GqStatisticsConfig selectTotal() {
        GqStatisticsConfig bean = new GqStatisticsConfig();
        bean.setActivationTotal(userCardMapper.selectActiveTotal());
        bean.setNotActiveTotal(userCardMapper.selectNotActiveTotal());
        bean.setUserTotal(userCardMapper.selectUserTotal());
        bean.setHospitalTotal(userCardMapper.selectHospitalTotal());
        bean.setRealNameTotal((int) gqRealnameInfoService.count());
        return bean;
    }
}
